package com.silverhaze.diamond.client.ui.table;

import com.silverhaze.diamond.client.ui.dialogs.view.ViewDialog;
import com.silverhaze.diamond.client.ui.table.model.DetailsTableModel;
import com.silverhaze.diamond.client.ui.table.renderers.DetailTableRenderer;

import javax.swing.*;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Comparator;

/**
 * Created by IntelliJ IDEA.
 * SystemUser: Ivan
 * Date: 20.07.2008
 * Time: 11:07:11
 * To change this template use File | Settings | File Templates.
 */
public class DetailsTable extends AbstractServiceTable {
    public DetailsTable(TableModel dm) {
        super(dm);

        initialize();
    }

    private void initialize() {
        setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

        setAutoCreateRowSorter(true);
        ((TableRowSorter) getRowSorter()).setComparator(0, new Comparator<Object>() {
            public int compare(Object o1, Object o2) {
                return ((Long) o1).compareTo((Long) o2);
            }
        });

        setDefaultRenderer(Object.class, new DetailTableRenderer());

        addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() >= 2) {
                    ViewDialog.detail(((DetailsTableModel) getModel()).getObjectAtRow(getSelectedRow()));
                }
            }
        });
    }
}

